var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');

var qqmapsdk = new QQMapWX({
  key: 'QPEBZ-XKJ64-WU7U6-XLCWL-CTNA6-OKFFD' // 必填
});

Page({
  data: {
    markers: [{
      iconPath: "/images/location.png",
      id: 0,
      latitude: 30.578235,
      longitude: 104.191543,
      width: 50,
      height: 50
    }, {
      iconPath: "/images/location.png",
      id: 1,
      latitude: 30.574514,
      longitude: 104.192502,
      width: 50,
      height: 50
    }],
    polyline: [],
    latitude: 30.578235,
    longitude: 104.191543
  },
  onReady() {
    var _this = this;
    //调用距离计算接口
    qqmapsdk.direction({
      mode: 'driving',//可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址
      from:{
        latitude: 30.578235,
        longitude: 104.191543
      },
      to: {
        latitude: 30.574514,
        longitude: 104.192502,
      }, 
      success: function (res) {
        console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        _this.setData({
          latitude:pl[0].latitude,
          longitude:pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      }
    });
  }
})